package com.banciyuan.bcywebview.biz.post.cos;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.view.toast.MyToast;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.Character;

/**
 * Created by squall on 2015/12/30.
 */
public class CosCharacterAdapter extends BaseAdapter {

    private LayoutInflater layoutInflater;
    private List<de.greenrobot.daoexample.model.Character> clist = new ArrayList<Character>();
    private Context mContext;
    private CosPostActivity.ShowAdd showAdd;
    private boolean focusFlag = false;

    public CosCharacterAdapter(List<Character> clist, Context mContext, CosPostActivity.ShowAdd showAdd) {
        this.clist = clist;
        this.mContext = mContext;
        this.layoutInflater = LayoutInflater.from(mContext);
        this.showAdd = showAdd;
    }

    @Override
    public int getCount() {
        return clist.size();
    }

    @Override
    public Object getItem(int position) {
        return clist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private class ViewHolder {
        private EditText mCharacter;
        private EditText rCharacter;
        private ImageView minus;

        public ViewHolder(View convertView) {
            mCharacter = (EditText) convertView.findViewById(R.id.character_mname);
            rCharacter = (EditText) convertView.findViewById(R.id.character_rname);
            minus = (ImageView) convertView.findViewById(R.id.character_minus);
        }
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.post_cos_character_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Character character = clist.get(position);
        viewHolder.mCharacter.setText(character.getCharacter());
        viewHolder.rCharacter.setText(character.getCoser());
        viewHolder.minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAdd.show();
                if (clist.size() > 1) {
                    clist.remove(position);
                    notifyDataSetChanged();
                } else {
                    MyToast.show(mContext, mContext.getString(R.string.atleast_one));
                }
            }
        });
        return convertView;
    }

    public void changeFocusFlag() {
        this.focusFlag = true;
    }

}
